/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.runtime.Event.*;
	
	import flash.display.DisplayObjectContainer;
	
	import mx.controls.DateField;
	import mx.events.DropdownEvent;
	import mx.formatters.DateFormatter;
	
	/**
	 * 继承自DateField，并实现了IEDIComponent 借口
	 * 
	 */	
	public class EDIDateField extends DateField implements IEDIComponent
	{
		private var _field:String;
		private var _fieldtype:String;
		private var _notNull:Boolean;
		private var _table:String;
		private var _primaryKey:String="";
		private var _fieldLength:int;
		private var df:DateFormatter;
		
		/**
		 * 构造 日期控件 ，并将相关文字信息转换为中文显示
		 * <EDIDateField id="dTxtStartDate"/>
		 */		
		public function EDIDateField()
		{
			super();
			df=new DateFormatter;
			yearNavigationEnabled=true; 
			   
    		dayNames=['日','一','二','三','四','五','六']; 
    		monthNames=['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']; 
			formatString="YYYY-MM-DD"
			this.addEventListener(DropdownEvent.CLOSE,closeHandler);
			this.addEventListener(DropdownEvent.OPEN,openHandler);
		}
		
		/**
		 * 设定日期显示的格式 默认为YYYY-MM-DD 
		 * @param value
		 * 
		 */		
		override public function set formatString(value:String):void
		{
			super.formatString=value;
			if(value!=null)
			{
				df.formatString=value;
			}
		}  
		
		protected function  closeHandler(event:DropdownEvent):void
		{
			setHTMLPlanvisible(true);
		}
		
		protected function  openHandler(event:DropdownEvent):void
		{
			setHTMLPlanvisible(false);
		}
		
		private function setHTMLPlanvisible(value:Boolean):void
		{
			if(this.parentApplication!=null)
			{
				if(this.parentApplication is EDIApplication)
				{
					if(!value)
						EDIApplication.setHtmlPlanVisible(value,this);
					else
						EDIApplication.rstoreHtmlPlanVisible(value,this);
				}
				var parentPlan:DisplayObjectContainer=this.parent;
				
				while(this.parentApplication!=parentPlan && parentPlan!=null)
	   			{
	   				if(parentPlan is EDICanvas)
	   				{
	   					(parentPlan as EDICanvas).setHtmlPlanVisible(value);
	   				}
	   				parentPlan=parentPlan.parent;
	   			}
			}
		}
		/**
		 * 实现IEDIComponent接口的属性，设置表名。 
		 * @param value 表名
		 * 
		 */		
		public function set table(value:String):void
		{
			_table=value;
			EDIEventProxy.listenerDataRolling(value,this);
		}
		public function get table():String
		{return _table;}
		
		/**
		 * 实现IEDIComponent接口的属性，设置是否是主键 
		 * @param value
		 * 
		 */		
		public function set primaryKey(value:String):void
		{_primaryKey=value;}
		public function get primaryKey():String
		{return _primaryKey;}
		
		/**
		 *  实现IEDIComponent接口的属性，设置字段名 
		 * @param value
		 * 
		 */		
		public function set field(value:String):void
		{_field=value;}
		public function get field():String
		{return _field;}
		
		/**
		 * 实现IEDIComponent接口的属性，设置字段类型 
		 * @param value
		 * 
		 */		
		public function set fieldType(value:String):void
		{_fieldtype=value;}
		public function get fieldType():String
		{return _fieldtype;}
		
		/**
		 * 实现IEDIComponent接口的属性，设置字段长度 
		 * @param value
		 * 
		 */		
		public function set fieldLength(value:int):void{_fieldLength=value;}
		public function get fieldLength():int{return _fieldLength;}
		
		/**
		 * 是否允许为空  
		 * @param value
		 * 
		 */		
		public function set notNull(value:Boolean):void
		{_notNull=value;}
		
		public function get notNull():Boolean
		{return _notNull;}
		
		/**
		 * 设置值 
		 * @param value
		 * 
		 */		
		override public function set text(value:String):void
		{
			value=df.format(value);
			super.text=value;
		}
		
		/**
		 * 实现IEDIComponent接口的属性，捕获数据滚动 
		 * @param event
		 * 
		 */		
		public function dataRollingHandler(event:EDIDataEvent):void
		{
			text=event.value[field.toLowerCase()];
		}
		
		/**
		 * 返回数据绑定时所需要的值  
		 * @return 
		 * 
		 */		
		public function get ediValue():String
		{
			return text;
		}
		
	}
}